/**
 * 
 */
package resources;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;

import cii_lib.CCII_ACCESS_INTERF;
import cii_lib.CCII_COMPONENT;
import cii_lib.CCII_CONFIG_TABLE;

/**
 * @author pdrabik
 *
 */
public class UltimateOPS {
	
	public static CCII_ACCESS_INTERF getInterfaceByName(Vector<CCII_ACCESS_INTERF> from, String name) {
		for (int i = 0;i<from.size();i++) {
			if (from.get(i).Name().equals(name)) return from.get(i);
		}
		return null;
	}
	
	public static String getCIIHTMLTooltip(CCII_CONFIG_TABLE tab, String addInfo) {
		String adinfo = (addInfo==null)?"":new String("<br>" + "<u>"+addInfo + "</u>");
		return new String("<html><body bgcolor=\"#FBEC5D\"><table width=110><tr><td bgcolor=\"blue\"><font color=\"#FFFF00\">"
				+ "<b>" + tab.Name + "</b>"
				+ adinfo
				+ "</font></td></tr></table><b>Access: "
				+ tab.Access
				+ "</b><br>Address: "
				+ tab.Address+ ":"+ tab.Position 
//				+ "<center><img src=" + ("file:D:\\3-1b.gif")+ "></center>"
				+ "</body></html>");
	}
	
	public static String getTESTToolTip() {
		String adinfo = new String("<br>" + "<u>"+"&lt;info&gt;"+ "</u>");
		return new String("<html><body bgcolor=\"#FBEC5D\"><table width=110><tr><td bgcolor=\"blue\"><font color=\"#FFFF00\">"
				+ "<b>" + "&lt;NAME&gt;" + "</b>"
				+ adinfo
				+ "</font></td></tr></table><b>Access: "
				+ "&lt;TYP&gt;"
				+ "</b><br>Address: "
				+ "&lt;Adres&gt;" 
//				+ "<center><img src=" + ("file:D:\\3-1b.gif")+ "></center>"
				+ "</body></html>");
	
	}
	
	//TODO rekurencyjnie napisac
	public static DefaultMutableTreeNode buildTreeModel(CCII_COMPONENT comp) {
		DefaultMutableTreeNode rootComp = new DefaultMutableTreeNode("not loaded");
			rootComp = new DefaultMutableTreeNode(comp);
			DefaultMutableTreeNode child=null;
		    DefaultMutableTreeNode grandChild=null;
		    DefaultMutableTreeNode grandGrandChild=null;
		    int daq_childs=comp.getChildren().size();
		    for (int i = 0; i <daq_childs;i++) {
		    	CCII_COMPONENT cChild = comp.getChildren().get(i);
		    	child = new DefaultMutableTreeNode(cChild);
			    rootComp.add(child);
			    int fifo_childs=comp.getChildren().get(i).getChildren().size();
			    for (int ii=0;ii<fifo_childs;ii++) {
			    	CCII_COMPONENT fChild = comp.getChildren().get(i).getChildren().get(ii);
			    	grandChild = new DefaultMutableTreeNode(fChild);
			    	child.add(grandChild);
			    	int more_childs = fChild.getChildren().size();
			    	for (int iii=0;iii<more_childs;iii++) {
				    	CCII_COMPONENT mChild = fChild.getChildren().get(iii);
				    	grandGrandChild = new DefaultMutableTreeNode(mChild);
				    	grandChild.add(grandGrandChild);				    	
				    }
			    }
		    }
		    return rootComp;
	}
	
	//TODO rekurencyjnie napisac
	public static DefaultMutableTreeNode buildTreeModelInt(CCII_COMPONENT comp) {
		DefaultMutableTreeNode rootComp = new DefaultMutableTreeNode("not loaded");
			rootComp = new DefaultMutableTreeNode(comp);
			DefaultMutableTreeNode child=null;
		    DefaultMutableTreeNode grandChild=null;
		    DefaultMutableTreeNode grandGrandChild=null;
		    int daq_childs=comp.getChildren().size();
		    for (int i = 0; i <daq_childs;i++) {
		    	CCII_COMPONENT cChild = comp.getChildren().get(i);
		    	child = new DefaultMutableTreeNode(cChild);
			    rootComp.add(child);
			    int fifo_childs=comp.getChildren().get(i).getChildren().size();
			    for (int ii=0;ii<fifo_childs;ii++) {
			    	CCII_COMPONENT fChild = comp.getChildren().get(i).getChildren().get(ii);
			    	grandChild = new DefaultMutableTreeNode(fChild);
			    	child.add(grandChild);
			    	int more_childs = fChild.getChildren().size();
			    	for (int iii=0;iii<more_childs;iii++) {
				    	CCII_COMPONENT mChild = fChild.getChildren().get(iii);
				    	grandGrandChild = new DefaultMutableTreeNode(mChild);
				    	grandChild.add(grandGrandChild);				    	
				    }
			    }
		    }
		    return rootComp;
	}
	
	public static DefaultTreeCellRenderer treeRenderer() {
		DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
	    renderer.setOpenIcon(new ImageIcon("D:\\_przegrywane_dyskD\\multimedia\\obrazy\\drive-slim-raid-4disks-16x16.png"));
	    renderer.setClosedIcon(new ImageIcon("D:\\_przegrywane_dyskD\\multimedia\\obrazy\\drive-slim-raid-4disks-16x16.png"));
	    renderer.setLeafIcon(new ImageIcon("D:\\_przegrywane_dyskD\\multimedia\\obrazy\\drive-slim-internal-750gb-16x16.png"));
	    return renderer;
	}
	
	public static Point centerLocation(int width, int height) {
		Toolkit toolkit = Toolkit.getDefaultToolkit(); 
		Dimension screenSize = toolkit.getScreenSize(); 
		int x = (screenSize.width - width) / 2; 
		int y = (screenSize.height - height) / 2; 
		return new Point(x,y);
	}
}
